package com.huaxin.hxcloud.work;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huaxin.common.BaseFragment;
import com.huaxin.common.Entity.SignHistoryDTO;
import com.huaxin.common.tools.Log;
import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.fragment.SignRecordFragment;
import com.huaxin.hxcloud.work.view.HxImageGroupView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author songxf
 *         created at 2017/7/17 14:13
 * @desc 考勤统计 adapter
 */


public class HxSignRecordAdapter extends RecyclerView.Adapter {
    private static final int ITEM_TYPE_DEFAULT = 0x00;
    private static final int ITEM_TYPE_INFO    = 0x01;
    private static final int ITEM_TYPE_EMPTY   = 0x02;

    private static final String TAG = "HxSignRecordAdapter";
    private       List<SignHistoryDTO.SignListBean> mSignHistory;
    private       Context                           mContext;
    private final LayoutInflater                    mInflater;
    private       SignRecordFragment                mSignRecordFragment;

    public HxSignRecordAdapter(SignRecordFragment signRecordFragment, Context context, List<SignHistoryDTO.SignListBean> signHistory) {
        this.mContext = context;
        this.mSignHistory = signHistory;
        this.mSignRecordFragment = signRecordFragment;
        mInflater = LayoutInflater.from(context);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_INFO) {
            return new ViewHolder(mInflater.inflate(R.layout.item_sign_record, parent,
                    false));
        } else if (viewType == ITEM_TYPE_EMPTY) {
            return new EmptyViewHolder(mInflater.inflate(R.layout.item_empty_face_top, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).update(position);
        } else if (holder instanceof EmptyViewHolder) {
            ((EmptyViewHolder) holder).update(position);
        }
    }

    @Override
    public int getItemCount() {
        Log.d(TAG, "getItemCount: " + mSignHistory.size());
        return mSignHistory == null ? 0 : mSignHistory.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mSignHistory.size() > 0) {
            Log.d(TAG, "--1-->" + ITEM_TYPE_INFO);
            return ITEM_TYPE_INFO;
        } else if (mSignHistory == null || mSignHistory.size() == 0) {
            Log.d(TAG, "--2-->" + ITEM_TYPE_EMPTY);
            return ITEM_TYPE_EMPTY;
        }
        Log.d(TAG, "--3-->" + ITEM_TYPE_EMPTY);
        return super.getItemViewType(position);
    }


    class EmptyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_tip)
        TextView tipTv;

        public EmptyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void update(int position) {
            tipTv.setText("该月无签到记录");
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.check_path_lv)
        LinearLayout     mCheckPathLv;
        @BindView(R.id.station_name)
        TextView         mStationName;
        @BindView(R.id.work_time_tv)
        TextView         mWorkTimeTv;
        @BindView(R.id.title)
        LinearLayout     mTitle;
        @BindView(R.id.signin_time)
        TextView         mSigninTime;
        @BindView(R.id.signin_recyclerview)
        RecyclerView     mSigninRecyclerview;
        @BindView(R.id.signback_recyclerview)
        RecyclerView     mSignBackRecyclerview;
        @BindView(R.id.signin_des_tv)
        TextView         mSigninDesTv;
        @BindView(R.id.signback_des_tv)
        TextView         mSignBackDesTv;
        @BindView(R.id.signin_address)
        TextView         mSigninAddress;
        @BindView(R.id.signin_content_rv)
        RelativeLayout   mSigninContentRv;
        @BindView(R.id.signback_time)
        TextView         mSignbackTime;
        @BindView(R.id.signback_address)
        TextView         mSignbackAddress;
        @BindView(R.id.signin_img)
        HxImageGroupView mSigninImg;
        @BindView(R.id.signback_img)
        HxImageGroupView mSignBackImg;
        private SignHistoryDTO.SignListBean mSignListBean;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


        public void update(int position) {
            mSignListBean = mSignHistory.get(position);
            mSignRecordFragment.loadText(mStationName, mSignListBean.getStationName());

            if (mSignListBean.getInDate() == 0){
                mSigninTime.setText("签到时间：无");
            }else{
                mSignRecordFragment.loadText("签到时间：", mSigninTime, mSignRecordFragment.dateFormat(mSignListBean.getInDate
                        (), "HH:mm"), "无");
            }
            mSignRecordFragment.loadText(mSigninAddress, mSignListBean.getInAddress());
            if (mSignListBean.getBackDate() == 0){
                mSignbackTime.setText("签退时间：无");
            }else{
                mSignRecordFragment.loadText("签退时间：", mSignbackTime, mSignRecordFragment.dateFormat(mSignListBean
                        .getBackDate(), "HH:mm"), "无");
            }
            if (mSignListBean.getBackDate() !=0 && mSignListBean.getInDate() !=0){
          //      mSignRecordFragment.loadText("工作时长", mWorkTimeTv, mSignRecordFragment.getDiffHour(mSignListBean.getBackDate(), mSignListBean.getInDate()), "无");
                mWorkTimeTv.setText("(工作时长"+ BaseFragment.getDiffHour(mSignListBean.getBackDate(),mSignListBean.getInDate())+")");
            }else{
                mWorkTimeTv.setText("");
            }
            mSignRecordFragment.loadText(mSignbackAddress, mSignListBean.getBackAddress(), "无");

            mSigninRecyclerview.setLayoutManager(new GridLayoutManager(mContext, 4));
            mSignBackRecyclerview.setLayoutManager(new GridLayoutManager(mContext, 4));
            mSigninRecyclerview.setHasFixedSize(true);
            mSignBackRecyclerview.setHasFixedSize(true);


            List<String> flagBackTypes = new ArrayList<>();
            List<String> flagInTypes = new ArrayList<>();
            HxSignRecordTypeAdapter backAdapter = new HxSignRecordTypeAdapter(mContext, flagBackTypes);
            HxSignRecordTypeAdapter inAdapter = new HxSignRecordTypeAdapter(mContext, flagInTypes);
            mSigninRecyclerview.setAdapter(inAdapter);
            mSignBackRecyclerview.setAdapter(backAdapter);


            try {
                //签到type
                List<SignHistoryDTO.SignListBean.InTypeIdsBean> inTypeIds = mSignListBean.getInTypeIds();
                for (int i = 0; i < inTypeIds.size(); i++) {
                    flagInTypes.add(inTypeIds.get(i).getDes());
                }
                inAdapter.setData(flagInTypes);
                //签到描述
                mSignRecordFragment.loadText("描述:", mSigninDesTv, mSignListBean.getInDes(), "无");
                List<SignHistoryDTO.SignListBean.InImgUrlsBean> inImgUrls = mSignListBean.getInImgUrls();

                List<String> inImgs = new ArrayList<>();
                for (int i = 0; i < inImgUrls.size(); i++) {
                    inImgs.add(inImgUrls.get(i).getInImgUrl());
                }
                mSigninImg.setImageUrlList(inImgs);

                //签退
                List<SignHistoryDTO.SignListBean.BackTypeIdsBean> backTypeIds = mSignListBean.getBackTypeIds();
                for (int i = 0; i < backTypeIds.size(); i++) {
                    flagBackTypes.add(backTypeIds.get(i).getDes());
                }
                backAdapter.setData(flagBackTypes);
                //签退描述
                mSignRecordFragment.loadText("描述:", mSignBackDesTv, mSignListBean.getBackDes(), "无");
                List<SignHistoryDTO.SignListBean.BackImgUrlsBean> backImgUrls = mSignListBean.getBackImgUrls();
                List<String> backImgs = new ArrayList<>();
                for (int i = 0; i < backImgUrls.size(); i++) {
                    backImgs.add(backImgUrls.get(i).getBackImgUrl());
                }
                mSignBackImg.setImageUrlList(backImgs);
            } catch (Exception e) {

            }


        }

        @OnClick({R.id.check_path_lv})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.check_path_lv://站点
                    Intent intent = new Intent(mContext, HxRoutePlanActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("SIGN_HISTORY", (ArrayList<? extends Parcelable>) mSignHistory);
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                    break;
            }
        }
    }
}
